using System;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Configuration;
using Microsoft.SharePoint.Utilities;
using System.Reflection;

namespace CKS.FormsBasedAuthentication
{   
    /// <summary>
    /// This class is a feature activation event handler for the Admin Management feature.
    /// </summary>
    class AdminManagementFeatureReceiver : SPFeatureReceiver
    {        

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // get site reference
            SPSite site = properties.Feature.Parent as SPSite;

            /* bms Get a reference to the web */
            // get a web reference
            SPWeb web = site.OpenWeb(); 
            /* bms Set the URL strings in the web properties */
            //TODO: Make these resources
            UpdateProperty(MembershipReviewSiteURL.CHANGEPASSWORDPAGE,"Pages/ChangePassword.aspx",web);
            UpdateProperty(MembershipReviewSiteURL.PASSWORDQUESTIONPAGE,"Pages/PasswordQuestion.aspx",web);
            UpdateProperty(MembershipReviewSiteURL.THANKYOUPAGE,"Pages/Thankyou.aspx",web);

            /* bms Set the XSLT location web properties */
            //TODO: Make these resources

            UpdateProperty(MembershipReviewSiteXSLTEmail.MEMBERSHIPAPPROVED,"/_layouts/FBA/emails/MembershipApproved.xslt",web);
            UpdateProperty(MembershipReviewSiteXSLTEmail.MEMBERSHIPERROR,"/_layouts/FBA/emails/MembershipError.xslt",web);
            UpdateProperty(MembershipReviewSiteXSLTEmail.MEMBERSHIPPENDING,"/_layouts/FBA/emails/MembershipPending.xslt",web);
            UpdateProperty(MembershipReviewSiteXSLTEmail.MEMBERSHIPREJECTED,"/_layouts/FBA/emails/MembershipRejected.xslt",web);
            UpdateProperty(MembershipReviewSiteXSLTEmail.PASSWORDRECOVERY,"/_layouts/FBA/emails/PasswordRecovery.xslt",web);

            // update layouts site map
            try
            {
                UpdateLayoutsSitemap uls = new UpdateLayoutsSitemap(site.WebApplication);
                uls.AddSitemap("layouts.sitemap.FBASelfManagement.xml");
                uls.SubmitJob();
            }
            catch (Exception ex)
            {
                Utils.LogError(ex);
            }
        }

        private void UpdateProperty(string key, string value, SPWeb currentWeb)
        {
            if (currentWeb.Properties.ContainsKey(key))
            {
                currentWeb.Properties[key] = value;
            }
            else
            {
                currentWeb.Properties.Add(key, value);

            }
            currentWeb.Properties.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {         
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties) 
        {                
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) 
        {         
        }
    }
}